# encoding:utf-8
import requests
import json

import base64

'''
easydl图像分类
'''

def fish_detect(img):
    request_url = " https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/testmymodel"

    headers = {'Content-Type': 'application/json'}

    ak = 'jANHeqQCf6OmUGhS6py8CL7Y'
    sk = 'ynAdWOuKaXCN75455Ghj1QyvI3jWkgmX'

    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(
        ak, sk)
    response = requests.get(host)
    s = response.json()

    if response:
        access_token = s["access_token"]

    request_url = request_url + "?access_token=" + str(access_token)

    params = {"image": img, "top_num": 3}
    body = json.dumps(params).encode('utf-8')

    response = requests.post(request_url, body, headers=headers)

    if response.status_code == 200:
        return response.json()


if __name__ == '__main__':
    f = open('static/images/shark.jpeg', 'rb')
    img = base64.b64encode(f.read())
    print(fish_detect(img))
